package com.example.agriapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.agriapp.R;
import com.example.agriapp.model.bean.Message;

import java.util.List;

// MessageAdapter.java
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
    private List<Message> messages;

    public MessageAdapter(List<Message> messages) {
        this.messages = messages;
    }

    public void updateData(List<Message> newMessages) {
        messages = newMessages;
        notifyDataSetChanged();
    }
    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_message, parent, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MessageViewHolder holder, int position) {
        Message message = messages.get(position);
        holder.bind(message);
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    class MessageViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivAvatar;
        private TextView tvSender;
        private TextView tvTag;
        private TextView tvContent;
        private TextView tvTime;

        public MessageViewHolder(View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvSender = itemView.findViewById(R.id.tv_sender);
           // tvTag = itemView.findViewById(R.id.tv_tag);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
        }

        public void bind(Message message) {
            // 设置固定头像
            ivAvatar.setImageResource(R.drawable.default_avatar);

            tvSender.setText(message.getUsername());
            //tvTag.setText(message.getTag());
            tvContent.setText(message.getContent());
            tvTime.setText(message.getTime());

            // 根据图片中的样式，标签可能为空
//            if (message.getTag().isEmpty()) {
//                tvTag.setVisibility(View.GONE);
//            } else {
//                tvTag.setVisibility(View.VISIBLE);
//            }
        }
    }
}


